package com.smartcruise.main.selfFragment.setting.offlineMap;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.smartcruise.R;
import com.smartcruise.base.BaseFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by FengChaoQun
 * on 2018/3/8 0008
 * 离线地图
 */

public class OfflineMapFragment extends BaseFragment implements OfflineMapContract.View {

    @BindView(R.id.leftArrow)
    ImageView leftArrow;
    @BindView(R.id.download_list_text)
    TextView downloadListText;
    @BindView(R.id.downloaded_list_text)
    TextView downloadedListText;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    Unbinder unbinder;

    private ExpandableListView mAllOfflineMapList;
    private ListView mDownLoadedList;
    private PagerAdapter mPageAdapter;
    private OfflineMapContract.Presenter mPresenter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getRootView(R.layout.fragment_offline_map);
        unbinder = ButterKnife.bind(this, rootView);
        setEnableRightSlide(false);
        initView();
        return rootView;
    }

    private void initView() {
        mAllOfflineMapList = (ExpandableListView) getLayoutInflater().inflate(R.layout.offline_province_listview, null);
        mDownLoadedList = (ListView) getLayoutInflater().inflate(R.layout.offline_downloaded_list, null);
        mPageAdapter = new OfflinePagerAdapter(viewPager, mAllOfflineMapList, mDownLoadedList);
        viewPager.setAdapter(mPageAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    downloadListText.setBackgroundColor(getResources().getColor(R.color.w0));
                    downloadedListText.setBackgroundColor(getResources().getColor(R.color.titleColor));
                } else {
                    downloadListText.setBackgroundColor(getResources().getColor(R.color.titleColor));
                    downloadedListText.setBackgroundColor(getResources().getColor(R.color.w0));
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mPresenter = new OfflineMapPresenter(this);
        mPresenter.start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.leftArrow)
    public void onLeftArrowClicked() {
        getActivity().finish();
    }

    @OnClick(R.id.download_list_text)
    public void onDownloadListTextClicked() {
        viewPager.setCurrentItem(0);
    }

    @OnClick(R.id.downloaded_list_text)
    public void onDownloadedListTextClicked() {
        viewPager.setCurrentItem(1);
    }

    @Override
    public void setCityList(OfflineListAdapter adapter) {
        // 为列表绑定数据源
        mAllOfflineMapList.setAdapter(adapter);
        // adapter实现了扩展列表的展开与合并监听
        mAllOfflineMapList.setOnGroupCollapseListener(adapter);
        mAllOfflineMapList.setOnGroupExpandListener(adapter);
        mAllOfflineMapList.setGroupIndicator(null);
    }

    @Override
    public void setDownedCityList(BaseAdapter adapter) {
        mDownLoadedList.setAdapter(adapter);
    }

    @Override
    public ViewPager getViewPager() {
        return viewPager;
    }

}
